package com.hn.gege.im.modules.im.controller;

import com.hn.gege.core.common.CommonResult;
import com.hn.gege.core.util.UpLoadUtil;
import com.hn.gege.im.config.util.AudioUtil;
import com.hn.gege.im.modules.im.service.IChatMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;

import java.io.IOException;
import java.util.Date;

@Api(tags = "WangEditUploadController", description = "聊天文件上传接口")
@RestController
@RequestMapping("/chatUpload")
@Validated
public class ChatUploadController {
        @Value(value="${file.http.path:D:/}")
        private String HPATH;
    @Autowired
    IChatMessageService chatMessageService;

    @ApiOperation("聊天图片上传")
    @PostMapping(value="/imgUpload",consumes="multipart/*", headers="content-type=multipart/form-data")
    public CommonResult<String> imgUpload(@RequestParam("file") MultipartFile file) throws IOException {

        Date date = new Date();
        String path = getFilePath(file);
        if (StringUtils.isEmpty(path)){
            path = UpLoadUtil.uploadBeltDate(file,"im/img");
        }
        Date date1 = new Date();
        System.out.println(date1.getTime() - date.getTime());
        String httpPath = HPATH + path;
        return CommonResult.success(httpPath);
    }

    @ApiOperation("聊天语音MP3上传")
    @PostMapping(value="/mp3Upload",consumes="multipart/*", headers="content-type=multipart/form-data")
    public CommonResult<String> mp3Upload(@RequestParam("file") MultipartFile file) throws IOException {
        String path = getFilePath(file);
        if (StringUtils.isEmpty(path)) {
            path = UpLoadUtil.uploadBeltDate(file, "im/mp3");
        }
        String httpPath = HPATH + path;
        return CommonResult.success(httpPath);
    }

    @ApiOperation("聊天语音上传")
    @PostMapping(value="/audioUpload",consumes="multipart/*", headers="content-type=multipart/form-data")
    public CommonResult<String> audioUpload(@RequestParam("file") MultipartFile file) throws IOException, EncoderException {

        String path = getFilePath(file);
        if (StringUtils.isEmpty(path)) {
            path = UpLoadUtil.uploadBeltDate(file, "im/audio");
        }

        Date date = new Date();
        Date date1 = new Date();
        System.out.println("图片上传时间" + (date1.getTime() - date.getTime()));


        String s = AudioUtil.changeTemp(path);


        Date date2 = new Date();
        System.out.println("图片转化时间" + (date2.getTime() - date1.getTime()));
        System.out.println(HPATH + s);


        String httpPath = HPATH + s;
        return CommonResult.success(httpPath);
    }

    /**
     * 检验该文件在服务器中是否存在,返回文件在服务器的相对路径
     * @param file
     * @return
     */
    private String getFilePath(MultipartFile file) throws IOException {
        String md5 = DigestUtils.md5Hex(file.getBytes());
        return chatMessageService.checkFileMD5(md5);
    }


}
